package cn.wolfcode.web.controller;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/empLogin")
    @ResponseBody
    public JsonResult empLogin(String username, String password, HttpSession session) {
        JsonResult jsonResult = new JsonResult();
        Employee emp = employeeService.empLogin(username, password);
        session.setAttribute("USER_IN_SESSION", emp);
        List<Permission> permissionList = permissionService.selectByPermission(emp.getId());
        session.setAttribute("PERMISSION_IN_SESSION", permissionList);
        return jsonResult;
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/login.html";
    }

    @RequestMapping("/nopermission")
    public String noPermission() {
        return "/common/nopermission";
    }

    @RequestMapping("/error")
    public String error() {
        return "/common/error";
    }
}
